
// if (!/key=[\u4e00-\u9fa5\w]+/.test(location.href)) {
    // location.href = '../html/list.html';
// }

// headul划入的效果
$('.headul li').hover(function () {
    // $(this).addClass('act').siblings().removerClass('act');
    $(this).addClass('act').siblings().removeClass('act');
}, function () {
    // console.log(1);
});
// 回到顶部
$(window).scroll(function () {
    if (scrollY > 220) {
        $('.top').show();
    } else {
        $('.top').hide();
    }
});
$('.top').click(function () {
    $('html').animate({
        scrollTop: 0
    }, 1000);
});

// 已登录
let denglu = document.querySelector('#login');
let login = getCookie('login');
login ? denglu.innerHTML =   login : '';
denglu.onclick = function () {
    location.href = '../html/login.html';
    localStorage.setItem('url',location.href);
    return;
}



// 退出登录
login ? $('.exitdegnlu').show() : $('.exitdegnlu').hide();
$('.exitdegnlu').click(function () {
    console.log(1);
    setCookie('login', '111', '-1');
    location.href = '../html/login.html';
    localStorage.setItem('url',location.href);
    return;
});


// 跳转到购物车
$('.car').click(function () {
    if (!login) {
        alert('请先登录，在2s后跳转到登录界面');
        setTimeout(() => {
             location.href = '../html/login.html';
        }, 1000);
        return;
    } else {
        location.href = '../html/mycar.html';
    }

});


// 列表生成
let idx = 1;
let row = document.querySelector('.row');
let btn = document.querySelector('.btn1');
let page = document.querySelector('.page');
var searchkey = document.querySelector('#searchkey');

let obj = {
    index: 1,
    len: 20,
}
async function getlist() {
    let res = await pAjax({
        url: '../../5.5fenye.php',
        data:{
            index: idx,
            len: 20,
        }
    });
    res = JSON.parse(res);
    // console.log(res);
    Runderlist(res.list);


    new Pagination(page, {
        pageInfo: {
            pagenum: 1,
            pagesize: obj.len,
            total: res.total,
            totalpage: Math.ceil(res.total / obj.len) // 页码总数
        },
        textInfo: {
            first: '第一页',
            prev: '上一页',
            list: '',
            next: '下一页',
            last: '最后一页'
        },
        change: function (index) {
            obj.index = index;
            newDate();
            row.innerHTML='';
        }
    });
    

}

getlist();


async function newDate() {
    let res = await new pAjax({
        url: '../../5.5fenye.php',
        data: {
            index:obj.index,
            len:obj.len,
        }
    });
    res = JSON.parse(res);
    // console.log(res);
    Runderlist(res.list);
}

function Runderlist(data) {
    let str = '';
    data.forEach(item => {
        str += `
        <div class="col-xs-2 col-md-3">
                <ol class="breadcrumb three">
                    <li><a href="#">${item.cat_one_id}</a></li>
                    <li><a href="#">${item.cat_two_id}</a></li>
                    <li class="active ">${item.cat_three_id}</li>
                </ol>
                <div class="thumbnail">
                    <a href="../html/detail.html?id=${item.goods_id}"><img src="${item.goods_big_logo}" alt="..."></a>
                    <div class="caption">
                      <h3 class='p1'>${item.goods_name}</h3>
                      <p ><i class="glyphicon glyphicon-usd"></i>${item.goods_price}</p>
                      <p><a href="../html/mycar.html" class="btn btn-primary" role="button">查看购物车</a> 
                        <a href="../html/detail.html?id=${item.goods_id}" class="btn btn-default  btn_default" role="button" >查看商品详情</a></p>
                    </div>
                  </div>

                </div>
        `;
        $('.row').html(str);
    });
}


  // 搜索过来的列表页
  if (localStorage.getItem('key')) {
    searchkey.value = localStorage.getItem('key');
    localStorage.removeItem('key');
    getsearch();
    console.log(1);
    }


// 搜索关键字
let searchkbtn = document.querySelector('#searchkbtn');
searchkbtn.onclick = getsearch;
 function getsearch() {
    
    if (!searchkey.value) {
        $('.searchtip').hide();
        getlist();
        console.log(2);
        return;
    }
    
    pAjax({
        url:'../../6.8search.php',
        data: {
            key:searchkey.value,
        }
    }).then(res => {
        res = JSON.parse(res);
        if (!res.code) {
            row.innerHTML = '暂时没有这个关键的商品，请重新搜索！！！';
            page.innerHTML = '';
            return;
        }
    
        $('.searchtip').show().html(`搜索到<i>${searchkey.value}</i>的商品有：`);
        console.log(res.list);
        Runderlist(res.list);
        page.innerHTML = '';
    });
   

}




